package app.android.com.wuandroidapp.test.presenter;

import android.content.Context;
import android.widget.Toast;

import javax.inject.Inject;

import app.android.com.wuandroidapp.library.util.RxSchedulerUtils;
import app.android.com.wuandroidapp.test.contract.PromodtionContract;
import app.android.com.wuandroidapp.test.model.repository.PromodtionRepository;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by admin on 2017/4/22.
 */

public class PromodtionPresenter implements PromodtionContract.Presenter {
    private final Context context;
    private final PromodtionContract.View promodtionView;
    private CompositeSubscription subscriptions;
    @Inject
    public PromodtionPresenter(Context context,PromodtionContract.View view){
        this.context = context;
        this.promodtionView = view;
        promodtionView.setPresenter(this);
        this.subscriptions = new CompositeSubscription();
    }
    @Override
    public void subscribe() {
        loadPromodtionDetail();
    }

    @Override
    public void unSubscribe() {
        subscriptions.clear();
    }

    @Override
    public void loadPromodtionDetail() {
        Subscription subscription= PromodtionRepository.getTest(context).compose(RxSchedulerUtils.normalSchedulersTransformer())
                .subscribe(promodtionView::displayTestInformation, throwable -> {
                    Toast.makeText(context, throwable.getMessage(), Toast.LENGTH_LONG).show();
                });
        subscriptions.add(subscription);
    }
}
